package com.platform.controller.shop;

import com.platform.entity.shop.UserRecordActivityEntity;
import com.platform.service.shop.UserRecordActivityService;
import com.platform.service.shop.UserRecordService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.excel.ExcelExport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author zhouhaisheng
 * @email
 * @date
 */
@RestController
@RequestMapping("userRecordActivity")
public class UserRecordActivityController {
    @Autowired
    private UserRecordActivityService userRecordActivityService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<UserRecordActivityEntity> userRecordList = userRecordActivityService.queryList(query);

        int total = userRecordActivityService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(userRecordList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 导出会员
     */
    @RequestMapping("/export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) {

        List<UserRecordActivityEntity> userList = userRecordActivityService.queryList(params);

        String exportFileName = "会员活动余额提现申请列表";
        if(params.get("cTime") != null){
            exportFileName += "_"+params.get("cTime");
        }
        ExcelExport ee = new ExcelExport(exportFileName);

        String[] header = new String[]{"用户id","会员名称", "姓名", "证件号码", "银行卡号", "银行卡开户行", "手机号", "提现金额", "可用余额", "提现申请时间"};

        List<Map<String, Object>> list = new ArrayList<>();

        if (userList != null && userList.size() != 0) {
            for (UserRecordActivityEntity userEntity : userList) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("userId", userEntity.getUserId());
                map.put("userName", userEntity.getUserName());
                map.put("realName", userEntity.getRealName());
                map.put("idCard", userEntity.getIdCard());
                map.put("bankCard", userEntity.getBankCard());
                map.put("openBank", "");
                map.put("mobile", userEntity.getMobile());
                map.put("price", userEntity.getPrice());// 提现金额（用户输入的）

                map.put("amount", userEntity.getAmount());
                map.put("ctime", userEntity.getCtime());

                list.add(map);
            }
        }

        ee.addSheetByMap("会员活动余额提现申请", list, header);
        ee.export(response);
    }

}
